package com.intelligence.tio.server.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.json.Json;

import com.intelligence.tio.common.Command;
import com.intelligence.tio.common.JsonPacket;
import com.intelligence.tio.common.TioSessionContext;
import com.intelligence.tio.common.intf.AbsTioBsHandler;
import com.intelligence.tio.common.packages.GroupMsgReqBody;
import com.intelligence.tio.common.packages.GroupMsgRespBody;

/**
* @Title: GroupMsgReqHandler.java 
* @Package com.intelligence.tio.server.handler 
* @Description: 发送组消息处理器
* @Company: 南京因坦利软件有限公司
* @author 郭浩柱  
* @date 2017年5月18日 下午3:30:43 
* @version V1.0
 */
public class GroupMsgReqHandler extends AbsTioBsHandler<GroupMsgReqBody>
{
	private static Logger log = LoggerFactory.getLogger(GroupMsgReqHandler.class);

	/** 
	 * @return
	 * @author: tanyaowu
	 */
	@Override
	public Class<GroupMsgReqBody> bodyClass()
	{
		return GroupMsgReqBody.class;
	}

	/** 
	 * @param packet
	 * @param bsBody
	 * @param channelContext
	 * @return
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@Override
	public Object handler(JsonPacket packet, GroupMsgReqBody bsBody, ChannelContext<TioSessionContext, JsonPacket, Object> channelContext) throws Exception
	{
		log.info("收到群聊请求消息:{}", Json.toJson(bsBody));
		
		
		//获取发送人信息
		TioSessionContext sessionContext = channelContext.getSessionContext();

		GroupMsgRespBody groupMsgRespBody = new GroupMsgRespBody();
		groupMsgRespBody.setText(bsBody.getText());
		groupMsgRespBody.setToGroupId(bsBody.getToGroupId());;
		groupMsgRespBody.setFromUserId(sessionContext.getUserId());
		groupMsgRespBody.setFromUserName(sessionContext.getUserName());
		groupMsgRespBody.setFromUserPhone(sessionContext.getUserPhone());
		groupMsgRespBody.setCommand(Command.COMMAND_SEND_GROUP_MSG_RESP.getValue());
		groupMsgRespBody.setFromRoleId(sessionContext.getRoleId());
		JsonPacket respPacket = new JsonPacket();
		respPacket.setBody(Json.toJson(groupMsgRespBody).getBytes(JsonPacket.CHARSET));
		Aio.sendToGroup(channelContext.getGroupContext(), bsBody.getToGroupId(), respPacket);
		return null;
	}
}
